<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar">
      <el-form
        :inline="true"
        :model="filters"
        :size="size"
        ref="filters"
        style="width: 100%"
      >
        <el-row class="search-input">
          <el-form-item prop="logisticsCompanyNo" label="物流公司编号：">
            <el-input
              v-model="filters.logisticsCompanyNo"
              clearable
              placeholder="物流公司编号："
            ></el-input>
          </el-form-item>
          <el-form-item prop="logisticsCompanyName" label="物流公司名称：">
            <el-input
              v-model="filters.logisticsCompanyName"
              clearable
              placeholder="物流公司名称："
            ></el-input>
          </el-form-item>
        </el-row>
        <el-row class="search-button">
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-plus"
              :label="$t('action.add')"
              type="primary"
              @click="handleAdd"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-refresh"
              :label="$t('action.reset')"
              type="primary"
              @click="resetForm('filters')"
            />
          </el-form-item>
          <el-form-item>
            <kt-button label="批量删除" type="primary" @click="handleBatchDelete"/>
          </el-form-item>
        </el-row>
        <el-form-item class="t_r w100">
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button
                icon="fa fa-refresh"
                @click="findPage(null)"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button
                icon="fa fa-filter"
                @click="displayFilterColumnsDialog"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button
                icon="fa fa-file-excel-o"
                @click="downloadExcel"
              ></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
        <table-column-filter-dialog
          ref="tableColumnFilterDialog"
          :columns="columns"
          @handleFilterColumns="handleFilterColumns"
        ></table-column-filter-dialog>
      </el-form>
    </div>
    <!--表格内容栏-->
    <Jic-table
      :height="350"
      :data="pageResult"
      :columns="filterColumns"
      @findPage="findPage"
      @handleEdit="handleEdit"
      @handleDelete="handleDelete"
      @handleDetail="handleDetail"
      @selectionChange="selectionChange"
      :showBatchDelete="false"
      ref="JicTable"
      :loading="pageLoading"
    ></Jic-table>
    <!--新增编辑界面-->
    <el-dialog :title=optStatus width="600px" :visible.sync="editDialogVisible" :close-on-click-modal="false">
      <el-form :model="dataForm" label-width="120px" :rules="dataFormRules" label-position="right" ref="dataForm" :size="size">
        <el-row class="searchDialog-input">
        <el-form-item label="配送商来源" prop="source">
          <el-select v-model="dataForm.source" placeholder="请选择配送商来源" style="width: 400px">
            <el-option label="系统配送商" value="1">系统配送商</el-option>
            <!--<el-option label="系统配送商" value="1">自定义配送电商</el-option>-->
          </el-select>
        </el-form-item>
        <el-form-item label="物流公司名称" prop="logisticsCompanyName">
          <el-input v-model="dataForm.logisticsCompanyName" placeholder="请输入物流公司名称" auto-complete="off" :disabled="false" style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label="物流公司编号" prop="logisticsCompanyNo">
          <el-input v-model="dataForm.logisticsCompanyNo" placeholder="请输入物流公司编号"  oninput="if(value.length>50)value=value.slice(0,50)"  auto-complete="off" :disabled="false" style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="telephone" >
          <el-input v-model="dataForm.telephone" placeholder="请输入联系电话" auto-complete="off" :disabled="false" style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="dataForm.status" placeholder="请选择状态" style="width: 400px">
            <el-option label="启用" value="1"></el-option>
            <el-option label="停用" value="0"></el-option>
          </el-select>
        </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button :size="size" @click.native="editDialogVisible = false">{{ $t("action.cancel") }}</el-button>
        <el-button :size="size" type="primary" @click.native="submitForm" :loading="editLoading">{{ $t("action.submit") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import PopupTreeInput from "@/components/PopupTreeInput";
  import JicTable from "@/views/Core/JicTable";
  import KtButton from "@/views/Core/KtButton";
  import {format, formatDate} from "@/utils/datetime";
  import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
  import {exportExcel} from "@/utils/excel";
  import "quill/dist/quill.core.css";
  import "quill/dist/quill.snow.css";
  import "quill/dist/quill.bubble.css";
  export default {
    components: {
      JicTable,
      KtButton,
      TableColumnFilterDialog,
      PopupTreeInput,
    },
    data() {
      return {
        optStatus:null,
        content: "",
        editorOption: {
          modules: {
            imageDrop: true,
          },
          theme: "snow",
        },
        size: "small",
        filters: {},
        columns: [],
        filterColumns: [],
        pageRequest: {
          pageNum: 1,
          pageSize: 10,
        },
        pageResult: {},

        operation: false, // true:新增, false:编辑
        editDialogVisible: false, // 新增编辑界面是否显示
        editLoading: false,
        dataFormRules: {
          source: [
            {
              required: true,
              message: "请选择配送商来源",
              trigger: "blur",
            },
          ],
          logisticsCompanyName: [
            {
              required: true,
              message: "请输入物流公司名称",
              trigger: "blur",
            },
          ],
          logisticsCompanyNo: [
            {
              required: true,
              message: "请输入物流公司编码",
              trigger: "blur",
            },
            {
              min: 2,
              max: 50,
              message: "长度在 2 到 50 个字符",
              trigger: "blur",
            },
            {
              pattern: /^[^\u4e00-\u9fa5]+$/,
              message: "虚拟库编码不能包含汉字",
              trigger: "blur",
            },
            {
              pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+$/,
              message: "虚拟库编码不能包含特殊字符",
              trigger: "blur",
            },
          ],
          telephone: [
            {
              required: true,
              message: "请输入联系电话",
              trigger: "blur",
            },
          ],
          status: [
            {
              required: true,
              message: "请选择状态",
              trigger: "blur",
            },
          ],
        },
        // 新增编辑界面数据
        dataForm: {
          logisticsCompanyName: "",
          logisticsCompanyNo: "",
          telephone: "",
          source: "",
          status: "",
        },
        shopList: [],
        statusList: [],
        checked: [],
        pageLoading: false, //列表加载
      };
    },
    methods: {
      // 获取分页数据
      findPage: function (data) {
        let this_ = this;
        if (this_.filters == undefined || this_.filters == null) {
          this_.filters = {};
        }
        this_.filters.start = this.pageRequest.pageNum;
        this_.filters.limit = this.pageRequest.pageSize;
        this_.pageLoading = true;
        this.utils.request.queryOrderLogisticsCompany(this_.filters, function (res) {
          this_.pageLoading = false;
          if (res.data == null) {
            res.data = [];
            res.totle = 0;
          }
          this_.$refs.JicTable.findPage({
            content: res.data.rows,
            total: res.data.total,
          });
        });
      },
      // 显示新增界面
      handleAdd: function () {
        this.optStatus="新增";
        this.editDialogVisible = true;
        this.operation = true;
        this.$refs['dataForm'].resetFields();
        this.dataForm={};
        // alert("add");
      },
      //选择框
      selectionChange: function (params) {
        this.checked = params.selections;
      },
      // 删除
      handleDelete: function (data) {
        let id = data.params[0].id;
        var params = {};
        params.ids = id;
        var this_ = this;
        this.utils.request.deleteOrderLogisticsCompany(params, function (res) {
          if (res.code == "0000") {
            this_.$message({
              message: "操作成功",
              type: "success",
            });
            this_.findPage(null);
          } else {
            this_.$message({
              message: "操作失败, " + res.msg,
              type: "error",
            });
          }
        });
      },
      // 批量删除
      handleBatchDelete: function (data) {
        console.log("选中数据" + this.checked)
        if (this.checked.length == 0) {
          this.$message({
            message: "请选择一条数据进行处理！",
            type: "error",
            center: true,
            offset: 250,
            duration: 1000,
          });
        } else {
          let params = {};
          let ids = this.checked.map(item => item.id).toString();
          console.log("批量删除id：" + ids);
          params.ids = ids;
          var this_ = this;
          this.utils.request.deleteOrderLogisticsCompany(params, function (res) {
          if (res.code == "0000") {
            this_.$message({
              message: "操作成功",
              type: "success",
            });
            this_.findPage(null);
          } else {
            this_.$message({
              message: "操作失败, " + res.msg,
              type: "error",
            });
          }
        });
        }

      },

      // 编辑页面
      handleEdit: function (params) {
        this.editDialogVisible = true;
        this.optStatus="编辑";
        this.dataForm = Object.assign({}, params.row);
        },

      // 详情页面
      handleDetail: function () {
        this.$router.push({
          path: "/MerchantEditPage",
          query: {
            cid: data.row.id,
          },
        });
      },
      // 编辑
      submitForm: function () {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
              let this_ = this;
              let params = Object.assign({}, this.dataForm);
              this.utils.request.editOrderLogisticsCompany(params, function (res) {
                if (res.code == "0000") {
                  this_.$message({
                    message: "操作成功",
                    type: "success",
                  });

                  this_.$refs["dataForm"].resetFields();

                  this_.editDialogVisible = false;

                  this_.findPage(null);
                } else {
                  this_.$message({
                    message: "操作失败, " + res.msg,
                    type: "error",
                  });
                }
              });

          }
        });
      },
      //启用停用
      editStatus: function () {
        if (this.checked.length == 0) {
          this.$message({
            message: "请选择一条数据进行处理！",
            type: "error",
            center: true,
            offset: 250,
            duration: 1000,
          });
        } else {
          let params = {};
          params.id = this.checked[0].id;
          if (this.checked[0].status == "0") {
            params.status = "1";
          } else if (this.checked[0].status == "1") {
            params.status = "0";
          }
          var this_ = this;
          this.utils.request.editChannelInfo(params, function (res) {
            this_.$message({
              message: "操作成功",
              type: "success",
              center: true,
              offset: 250,
              duration: 1000,
            });
            this_.findPage(null);
          });
        }
      },
      // 时间格式化
      dateFormat: function (row, column, cellValue, index) {
        return format(row[column.property]);
      },

      resetForm(formName) {
        if (this.$refs[formName] != undefined) {
          this.$refs[formName].resetFields();
          this.filters = {};
          this.findPage(null);
        }
      },

      // 处理表格列过滤显示
      displayFilterColumnsDialog: function () {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true);
      },
      // 处理表格列过滤显示
      handleFilterColumns: function (data) {
        this.filterColumns = data.filterColumns;
        this.$refs.tableColumnFilterDialog.setDialogVisible(false);
      },
      // 处理表格列过滤显示
      initColumns: function () {
        this.columns = [
          {
            prop: "logisticsCompanyName",
            label: "物流公司名称",
            minWidth: 150,
          },
          {
            prop: "logisticsCompanyNo",
            label: "物流公司编码",
            minWidth: 120,
          },
          {
            prop: "telephone",
            label: "联系电话",
            minWidth: 150,
          },
          {
            prop: "status",
            label: "状态",
            minWidth: 120,
            formatter: this.formatStatus
          },
          {
            prop: "source",
            label: "配送商来源",
            minWidth: 150,
            formatter: this.formatSource
          }
        ];
        var temp = [];

        $.each(this.columns, function (key, val) {
          temp.push(val);
        });
        this.filterColumns = temp;
      },
      online: function (row, column, cellValue, index) {
        if (cellValue == 1) {
          return "线上";
        } else if (cellValue == 0) {
          return "线下";
        }
      },
      model: function (row, column, cellValue, index) {
        if (cellValue == 1) {
          return "BBC";
        } else if (cellValue == 2) {
          return "O&O";
        } else if (cellValue == 3) {
          return "B2C";
        } else if (cellValue == 4) {
          return "京东";
        } else if (cellValue == 5) {
          return "天猫";
        } else if (cellValue == 6) {
          return "B2B";
        }
      },
      type: function (row, column, cellValue, index) {
        if (cellValue == 1) {
          return "自建渠道";
        } else if (cellValue == 2) {
          return "第三方电商";
        }
      },
      status: function (row, column, cellValue, index) {
        if (cellValue == 1) {
          return "启用";
        } else if (cellValue == 0) {
          return "停用";
        }
      },
      // 配送公司设置  状态
      formatStatus(row, column, cellValue, index) {
        if (row.status == 1) {
          return "启用";
        } else if (row.status == 0) {
          return "停用";
        }
      },
      formatSource: function (row, column, cellValue, index) {
        if (cellValue == 1) {
          return "系统配送商";
        } else if (cellValue == 2) {
          return "自定义配送商";
        }
      },
      //列表下载
      downloadExcel() {
        this.$confirm("确定下载列表文件?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.exportData(this.filters, "商家管理");
          })
          .catch(() => {
          });
      },
      //excle导出
      exportData: function (filters, fileName) {
        let this_ = this;
        if (filters == undefined || filters == null) {
          filters = {};
        }
        filters.start = this.pageRequest.pageNum;
        filters.limit = this.pageRequest.pageSize;
        this.utils.request.queryChannelList(this_.filters, function (res) {
          debugger;
          //将查询到的数据处理
          for (let i = 0; i < res.data.rows.length; i++) {
            res.data.rows[i].channelType = this_.type(
              null,
              null,
              res.data.rows[i].channelType,
              null
            );
            res.data.rows[i].channelModal = this_.model(
              null,
              null,
              res.data.rows[i].channelMode,
              null
            );
            res.data.rows[i].isOnline = this_.online(
              null,
              null,
              res.data.rows[i].isOnline,
              null
            );
            if (res.data.rows[i].status === "true") {
              res.data.rows[i].status = "启用";
            } else {
              res.data.rows[i].status = "禁用";
            }
          }
          fileName = fileName + "_" + formatDate(new Date(), "yyyyMMdd");
          exportExcel(res.data.rows, this_.filterColumns, fileName);
        });
      },
    },
    mounted() {
      this.initColumns();
      this.findPage();
    },
  };
</script>

<style lang="scss" scoped>
  .search-button {
    .el-form-item {
      &:last-of-type {
        margin-right: 0px !important;
      }
    }
  }
</style>
